﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.ObjectBuilder;
using Metro.Admin.Views;
using Metro.Admin.BusinessEntities;
using System.Collections.Generic;

public partial class Admin_Parameters : Microsoft.Practices.CompositeWeb.Web.UI.Page, IParameters
{
    private ParametersPresenter _presenter;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this._presenter.OnViewInitialized();
        }
        this._presenter.OnViewLoaded();
    }

    [CreateNew]
    public ParametersPresenter Presenter
    {
        set
        {
            this._presenter = value;
            this._presenter.View = this;
        }
    }

    #region IParameters Members

    public void ShowOthersParameters(System.Collections.Generic.ICollection<Metro.Admin.BusinessEntities.Parameter> parameters)
    {
        ParametersContainerDataSource.DataSource = parameters;
        ParametersGridView.Visible = true;
    }

    public void ShowFactoryParameters(System.Collections.Generic.ICollection<Metro.Admin.BusinessEntities.Parameter> parameters)
    {
        //foreach (Metro.Admin.BusinessEntities.Parameter param in parameters)
        //{
        //    if (param.Name.Equals("openSpot"))
        //        txtOpenSpot.Text = param.Value;
        //    if (param.Name.Equals("closeSpot"))
        //        txtCloseSpot.Text = param.Value;
        //    if (param.Name.Equals("openNextDay"))
        //        txtOpenNextDay.Text = param.Value;
        //    if (param.Name.Equals("closeNextDay"))
        //        txtCloseNextDay.Text = param.Value;
        //    if (param.Name.Equals("minPrice"))
        //        txtMinPrice.Text = param.Value;
        //    if (param.Name.Equals("maxPrice"))
        //        txtMaxPrice.Text = param.Value;
        //    if (param.Name.Equals("minAmount"))
        //        txtMinAmount.Text = param.Value;
        //    if (param.Name.Equals("maxAmount"))
        //        txtMaxAmount.Text = param.Value;
        //    if (param.Name.Equals("taxBase"))
        //        txtTaxBase.Text = param.Value;
        //    if (param.Name.Equals("suspend"))
        //        chbSuspend.Checked = bool.Parse(param.Value);

        //}
    }

    public void CreateParameter(Metro.Admin.BusinessEntities.Parameter parameter)
    {
        parameter.Type = "O";  
        this._presenter.CreateParameter(parameter);
    }

    public void UpdateParameter(Metro.Admin.BusinessEntities.Parameter parameter)
    {
        this._presenter.UpdateParameter(parameter);
    }

    public void DeleteParameter(Metro.Admin.BusinessEntities.Parameter parameter)
    {
        this._presenter.DeleteParameter(parameter);
    }

    #endregion
    protected void ParametersContainerDataSource_Deleted(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
    {
        this.DeleteParameter((Metro.Admin.BusinessEntities.Parameter)e.Instance);   
    }
    protected void ParametersContainerDataSource_Inserted(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
    {
        this.CreateParameter((Metro.Admin.BusinessEntities.Parameter)e.Instance);  
    }
    protected void ParametersContainerDataSource_Updated(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
    {
        this.UpdateParameter((Metro.Admin.BusinessEntities.Parameter)e.Instance);  
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            this.UpdateFactorySettings();
        }
        catch (Exception ex)
        {
            lblExceptions.Text = ex.Message;  
        }
    }

    private void UpdateFactorySettings()
    {
        Metro.Admin.BusinessEntities.Parameter parameter = null;
        List<Metro.Admin.BusinessEntities.Parameter> parameterList = null;
        try
        {
            parameter =new Metro.Admin.BusinessEntities.Parameter();
            parameterList = new List<Metro.Admin.BusinessEntities.Parameter>(); 
            
            parameter.Name = "openSpot";
            parameter.Value = txtOpenSpot.Text;
            parameter.Type = "F";

            this.UpdateParameter(parameter);
            parameterList.Add(parameter);  

            parameter = new Metro.Admin.BusinessEntities.Parameter();

            parameter.Name = "closeSpot";
            parameter.Value = txtCloseSpot.Text;
            parameter.Type = "F";

            this.UpdateParameter(parameter);
            parameterList.Add(parameter);  

            parameter = new Metro.Admin.BusinessEntities.Parameter();

            parameter.Name = "openNextDay";
            parameter.Value = txtOpenNextDay.Text;
            parameter.Type = "F";

            this.UpdateParameter(parameter);
            parameterList.Add(parameter);  

            parameter = new Metro.Admin.BusinessEntities.Parameter();

            parameter.Name = "closeNextDay";
            parameter.Value = txtCloseNextDay.Text;
            parameter.Type = "F";

            this.UpdateParameter(parameter);
            parameterList.Add(parameter);  

            parameter = new Metro.Admin.BusinessEntities.Parameter();

            parameter.Name = "minPrice";
            parameter.Value = txtMinPrice.Text;
            parameter.Type = "F";

            this.UpdateParameter(parameter);
            parameterList.Add(parameter);  

            parameter = new Metro.Admin.BusinessEntities.Parameter();

            parameter.Name = "maxPrice";
            parameter.Value = txtMaxPrice.Text;
            parameter.Type = "F";

            this.UpdateParameter(parameter);
            parameterList.Add(parameter);  

            parameter = new Metro.Admin.BusinessEntities.Parameter();

            parameter.Name = "minAmount";
            parameter.Value = txtMinAmount.Text;
            parameter.Type = "F";

            this.UpdateParameter(parameter);
            parameterList.Add(parameter);  

            parameter = new Metro.Admin.BusinessEntities.Parameter();

            parameter.Name = "maxAmount";
            parameter.Value = txtMaxAmount.Text;
            parameter.Type = "F";

            this.UpdateParameter(parameter);
            parameterList.Add(parameter);  

            parameter = new Metro.Admin.BusinessEntities.Parameter();

            parameter.Name = "taxBase";
            parameter.Value = txtTaxBase.Text;
            parameter.Type = "F";

            this.UpdateParameter(parameter);
            parameterList.Add(parameter);  

            parameter = new Metro.Admin.BusinessEntities.Parameter();

            parameter.Name = "suspend";
            parameter.Value = chbSuspend.Checked.ToString();
            parameter.Type = "F";

            this.UpdateParameter(parameter);
            parameterList.Add(parameter);  

            parameter = new Metro.Admin.BusinessEntities.Parameter();

            parameter.Name = "taxpercentage";
            parameter.Value = txtTaxPercentage.Text;   
            parameter.Type = "F";

            this.UpdateParameter(parameter);
            parameterList.Add(parameter);  

            parameter = new Metro.Admin.BusinessEntities.Parameter();

            parameter.Name = "transactionExpiration";
            parameter.Value = txtExpiration.Text;
            parameter.Type = "F";

            this.UpdateParameter(parameter);
            parameterList.Add(parameter);

            _presenter.ParametersNotification(parameterList);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}


